<?php
namespace app\admin\controller;
use think\Db;
class Ordergoods extends Base
{
    //列表
    public function lists()
    {
        if(is_post()){
            $cur_page     = input('page');
            $keyword  = input('keyword');
            $pay_status  = input('pay_status');
            $fahuo_status  = input('fahuo_status');
            $pay_method_id  = input('pay_method_id');
            $min_time = input('min_time');
            $max_time = input('max_time');
            $sql = "
            select
                [1]
            from
                order_goods a
            left join
                user b
            on
              a.user_id=b.id
            left join
                pay_method c
            on
              a.pay_method_id=c.id
            ";
            $param = [];
            $w = ['where'];
            if(!empty($min_time)){
                $w[] = ' and a.create_time>?';
                $param[] = strtotime($min_time);
            }
            if(!empty($max_time)){
                $w[] = ' and a.create_time<?';
                $param[] = strtotime($max_time);
            }
            if(!empty($keyword)){
                $w[] = ' and (a.title like ? or b.phone like ? or b.name like ?)';
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
            }
            if($pay_status!='100all'){
                $w[] = ' and a.pay_status = ?';
                $param[] = $pay_status;
            }
            if($fahuo_status!='100all'){
                $w[] = ' and a.fahuo_status = ?';
                $param[] = $fahuo_status;
            }
            if($pay_method_id!='100all'){
                $w[] = ' and a.pay_method_id = ?';
                $param[] = $pay_method_id;
            }
            #条件判断
            if (count($w)>1){
              $w[1] = str_replace("and","",$w[1]);
              $sql .= implode(" ",$w);
            }
            #分页
            $page_size = c('a.page_size');
            $page_html = com_list_page($sql, $param, $cur_page,$page_size);

            #内容
            $start = ($cur_page - 1) * $page_size;
            $field = "a.*,b.name,c.title as pay_method_title";
            $sql_list = str_replace("[1]",$field,$sql);
            $sql_list .= " order by a.id desc limit {$start},{$page_size}";
            $rslist = Db::query($sql_list, $param);
            foreach ($rslist as $k => $v) {
                $rslist[$k]['create_time'] = com_formate_time($v['create_time']);
                $rslist[$k]['checkbox'] = com_checkbox($v['id']);
                $rslist[$k]['pic'] = $v['pic'] ? "<img src='{$v['pic']}' height=50 />" : "";
                $rslist[$k]['pay_status'] = c('type.pay_status',$v['pay_status']);
                $rslist[$k]['fahuo_status'] = c('type.fahuo_status',$v['fahuo_status']);
                $rslist[$k]['action']  = com_btn_edit("/ordergoods/edit/id/{$v['id']}","","","查看");
                $rslist[$k]['action']  .= com_btn_del("/ordergoods/delete/id/{$v['id']}", "您确定要删除【{$v['title']}】？");
            }
            $body_html = com_list_body($rslist, ['checkbox','name','pic','title','rmb','pay_method_title','pay_status','fahuo_status','create_time','action']);
            com_check(['data'=>['com_body'=>$body_html, 'com_page'=>$page_html]]);
        }

        $method = Db::name('pay_method')->column('title','id');
        $d = [
          'pay_status'=>c('type.pay_status'),
          'fahuo_status'=>c('type.fahuo_status'),
          'pay_method_id' => $method,
        ];
        return view('',$d);
    }
    #编辑
    function edit($id){
      if(is_post()){
            $d = input();
            unset($d['file']);
            $rule = [
                'id'=>  'require',
                'title'=>  'require',
            ];
            $msg = [
              'id.require'   =>  '请输入id',
              'title.require'=>  '请输入标题',
            ];
            com_validate($rule, $msg, $d);
            $d['update_time'] = time();
            Db::name("order_goods")->update($d);
            com_check(['suc_msg'=>'修改成功!','suc_js'=>'alert|close|reload']);
      }
      $rs = Db::query("select a.*,b.name from order_goods a left join user b on a.user_id=b.id where a.id=?",[$id]);
      $info = $rs[0];
      $detail = Db::name('order_goods_detail')->where('order_goods_id',$id)->select();
      $rs = Db::query("select a.*,b.title from pay_data a left join pay_method b on a.pay_method_id=b.id where a.table=? and a.table_id=?",['order_goods',$id]);
      $pay_data = $rs ? $rs : [];
      $d = [
        'info'  => $info,
        'detail' => $detail,
        'pay_data' => $pay_data,
      ];
      return view('',$d);
    }
    #删除
    function delete($id=''){
        Db::name('order_goods')->where('id','in',[$id])->delete();
        com_check(['suc_msg'=>'删除成功!','suc_js'=>'alert|close|reload']);
    }
    #批量删除
    function delall(){
      if(is_post()){
          $ids = input('ids/a');
          Db::name('order_goods')->where('id','in',$ids)->delete();
          com_check(['suc_msg'=>'删除成功!','suc_js'=>'alert|close|reload']);
      }
    }
    //申请发货
    function sqsend($id){
        $rs = Db::name('order_goods_detail')->find($id);
        if($rs['is_sq']==1 || $rs['status']==1){
            com_error('状态不对!');
            com_check();
        }
        Db::name('order_goods_detail')->update(['id'=>$id,'is_sq'=>1]);
        $res = Db::name('order_goods_detail')->where('order_goods_id',$rs['order_goods_id'])->where('is_sq',0)->find();
        $fahuo_status = $res ? 1 : 2;
        Db::name('order_goods')->update(['id'=>$rs['order_goods_id'], 'fahuo_status'=>$fahuo_status]);

        com_check(['data'=>$id,'suc_msg'=>'申请成功!','suc_js'=>'alert|function.hide']);
    }
}
